﻿Module modDialog
    Public Sub Dialog_Choose(sourceObject As TextBox,
                             Optional kvpList As List(Of KeyValuePair(Of String, String)) = Nothing)

        If (sourceObject Is Nothing) Or Not Not (sourceObject.GetType Is GetType(TextBox)) Then Exit Sub
        Dim _textbox As TextBox = sourceObject
        Dim _tmp As String

        If frmMain.textboxTargetFileName.Enabled = True Then

            _tmp = Dialog_OpenFile()

            If _tmp = Nothing Then Exit Sub

            _textbox.Text = String_ReplaceToKVP(_tmp, kvpList)

        Else

            _tmp = Dialog_BrowseDirectory()

            If _tmp = Nothing Then Exit Sub

            _textbox.Text = String_ReplaceToKVP(_tmp, kvpList)

        End If
    End Sub

    Public Function Dialog_BrowseDirectory(Optional ByVal dialogInitialDirectory As String = Nothing) As String

        Dim dialog As New FolderBrowserDialog

        If Not (dialogInitialDirectory Is Nothing) Then
            'If System.IO.Directory.Exists(dialogInitialDirectory) Then dialog.RootFolder = dialogInitialDirectory
        End If

        If dialog.ShowDialog() <> DialogResult.OK Then
            Return Nothing
        Else
            Return dialog.SelectedPath
        End If
    End Function

    Public Function Dialog_OpenFile(Optional ByVal dialogTitle As String = Nothing,
                                    Optional ByVal dialogDefaultExt As String = Nothing,
                                    Optional ByVal dialogFilter As String = Nothing,
                                    Optional ByVal dialogInitialDirectory As String = Nothing) As String

        Dim dialog As New OpenFileDialog

        If Not (dialogTitle Is Nothing) Then
            dialog.Title = dialogTitle
        End If

        If Not (dialogDefaultExt Is Nothing) Then
            dialog.DefaultExt = dialogDefaultExt
        End If

        If Not (dialogFilter Is Nothing) Then
            dialog.Filter = dialogFilter
        End If

        If Not (dialogInitialDirectory Is Nothing) Then
            dialog.InitialDirectory = dialogInitialDirectory
        End If

        If dialog.ShowDialog() <> DialogResult.OK Then
            Return Nothing
        Else
            Return dialog.FileName
        End If
    End Function

    Public Function Dialog_SaveFile(Optional ByVal dialogOverwritePrompt As Boolean = True,
                                    Optional ByVal dialogTitle As String = Nothing,
                                    Optional ByVal dialogDefaultExt As String = Nothing,
                                    Optional ByVal dialogFilter As String = Nothing,
                                    Optional ByVal dialogInitialDirectory As String = Nothing) As String

        Dim dialog As New SaveFileDialog

        If Not (dialogTitle Is Nothing) Then
            dialog.Title = dialogTitle
        End If

        If Not (dialogDefaultExt Is Nothing) Then
            dialog.DefaultExt = dialogDefaultExt
        End If

        If Not (dialogFilter Is Nothing) Then
            dialog.Filter = dialogFilter
        End If

        If Not (dialogInitialDirectory Is Nothing) Then
            dialog.InitialDirectory = dialogInitialDirectory
        End If

        dialog.OverwritePrompt = dialogOverwritePrompt

        If dialog.ShowDialog() <> DialogResult.OK Then
            Return Nothing
        Else
            Return dialog.FileName
        End If
    End Function
End Module